<template>
  <div class="paswordPage">
    <div class="passwordBox">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="106px" class="demo-ruleForm">
        <el-form-item label="原始密码：" prop="prePass" class="passwordInputStyle">
          <el-input :key="passwordType" ref="prePass" v-model.trim="ruleForm.prePass" placeholder="请输入原始密码" :type="passwordType" autocomplete="off" />
          <span class="show-pwd" @click="showPwd('pre')">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item label="新密码：" prop="pass" class="passwordInputStyle">
          <el-input :key="newPasswordType" ref="newPass" v-model.trim="ruleForm.pass" placeholder="请输入新密码" :type="newPasswordType" autocomplete="off" />
          <span class="show-pwd" @click="showPwd('new')">
            <svg-icon :icon-class="newPasswordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
          <div class="tipText">
            数字与英文字母的任意组合
          </div>
        </el-form-item>
        <el-form-item label="确认新密码：" prop="checkPass" class="passwordInputStyle">
          <el-input :key="confirmPasswordType" ref="confirmPass" v-model.trim="ruleForm.checkPass" placeholder="请再次输入新密码" :type="confirmPasswordType" autocomplete="off" />
          <span class="show-pwd" @click="showPwd('confirm')">
            <svg-icon :icon-class="confirmPasswordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
          <el-button style="marginLeft:20px" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { modifyPassword } from '@/api/user'
import { RSA } from '@/utils/encryption'
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (value.length < 6) {
        callback(new Error('密码长度不可小于6个字符'))
      } else if (value.length > 18) {
        callback(new Error('密码长度不可大于18个字符'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value.length < 6) {
        callback(new Error('密码长度不可小于6个字符'))
      } else if (value.length > 18) {
        callback(new Error('密码长度不可大于18个字符'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      passwordType: 'password',
      newPasswordType: 'password',
      confirmPasswordType: 'password',
      ruleForm: {
        prePass: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        prePass: [
          { required: true, message: '请输入原始密码', trigger: 'blur' }
        ],
        pass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 密码显示或隐藏
    showPwd(v) {
      if (v === 'pre') {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.prePass.focus()
        })
      } else if (v === 'new') {
        if (this.newPasswordType === 'password') {
          this.newPasswordType = ''
        } else {
          this.newPasswordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.newPass.focus()
        })
      } else {
        if (this.confirmPasswordType === 'password') {
          this.confirmPasswordType = ''
        } else {
          this.confirmPasswordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.confirmPass.focus()
        })
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ChangePassword()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async ChangePassword() {
      await modifyPassword({
        confirmPassword: RSA(this.ruleForm.checkPass),
        newPassword: RSA(this.ruleForm.pass),
        oldPassword: RSA(this.ruleForm.prePass)
      }).then(res => {
        this.$message({ message: '修改成功,请重新登录', type: 'success' })
        this.$store.dispatch('user/logout')
        this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      }).catch(() => {
        this.resetForm('ruleForm')
      })
    }
  }
}
</script>

<style>
.paswordPage{
  margin: 15px;
  background-color: #fff;
}
.passwordBox{
  width: 400px;
  margin: 0 auto;
  padding: 100px 0;
}
.show-pwd {
  position: absolute;
  right: 10px;
  top: 2px;
  font-size: 16px;
  color: rgb(90, 78, 78);
  cursor: pointer;
  user-select: none;
}
.tipText {
  position: absolute;
  font-size: 12px;
  color: #999999;
  margin-top:5px ;
}
.passwordInputStyle {
  margin-bottom: 35px;
}
</style>
